﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverLight_Mini
{
  public partial class MainPage : UserControl
  {
    double i = 1.0;
    object currentSender;
    public MainPage()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
      System.Threading.Thread worker = new System.Threading.Thread(setOpacities);
      worker.Start();
    }

    private void setOpacities()
    {
      while (true)
      {
        this.Dispatcher.BeginInvoke(changeOpacity);
        System.Threading.Thread.Sleep(60);
      }
    }

    private void changeOpacity()
    {
      bool alter = true;
      for (int j = 0; j < LayoutRoot.Children.Count; j++)
      {
        if (LayoutRoot.Children[j].GetType().FullName == "System.Windows.Controls.Image")
        {
          Image img = ((Image)LayoutRoot.Children[j]);
          label1.Content = "Opacity = " + Math.Abs(Math.Cos(i)).ToString();
          label2.Content = "Slider Value = " + slider1.Value.ToString();
          img.Opacity = Math.Abs(Math.Tan(i));
          if (alter)
          {
            //img.Height = 20;
            //img.Width = 20;
          }
        }
        i = i + Double.Parse(slider1.Value.ToString());
      }
    }

    private void image142_MouseEnter(object sender, MouseEventArgs e)
    {
      currentSender = sender;
      //((Image)currentSender).Opacity = 1.0;
      System.Threading.Thread worker = new System.Threading.Thread(setOpacitiesNew);
      worker.Start();
    }

    private void setOpacitiesNew()
    {
      //for(double i = 0.0; i< 1.0 ; i = i + 0.05)
      {
        //this.Dispatcher.BeginInvoke(incrementOpacity, new object());
        this.Dispatcher.BeginInvoke(() => incrementOpacity());
        //System.Threading.Thread.Sleep(10);
      }
    }

    private void incrementOpacity()
    {
      Image img = ((Image)currentSender);
      img.Opacity = Math.Sqrt(Double.Parse(slider1.Value.ToString()));
      //if (img.Opacity < 0)
      //  img.Opacity = 1.0;
    }
  }
}